package com.fin.zw.intelligentbot.controller;

import com.fin.zw.intelligentbot.annotation.CurrentUser;
import com.fin.zw.intelligentbot.entity.CorpusInfo;
import com.fin.zw.intelligentbot.model.request.LibraryInfoRequest;
import com.fin.zw.intelligentbot.model.session.SessionModel;
import com.fin.zw.intelligentbot.service.CorpusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/corpus")
@Api(value = "语音库文件处理",description = "语音库文件处理")
public class CorpusController {

    @Autowired
    private CorpusService corpusService;

    @GetMapping("/fetchPage")
    @ApiOperation(value = "查询所有的语音库信息",notes = "查询所有的语音库信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query",
                    value = "页数 (0..N)"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query",
                    value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query",
                    value = "依据什么排序: 属性名(,asc|desc). ")
    })
    public ResponseEntity<Page<CorpusInfo>> queryLibraryInfo(Pageable pageable,
                                                             @ApiIgnore @CurrentUser SessionModel sessionModel) {
        Page<CorpusInfo> page = corpusService.queryLibraryInfo(pageable, sessionModel);
        return ResponseEntity.ok().body(page);
    }

    @GetMapping("/fetch")
    @ApiOperation(value = "查询所有的语音库信息",notes = "查询所有的语音库信息")
    public ResponseEntity<List<CorpusInfo>> queryLibraryInfo(@ApiIgnore @CurrentUser SessionModel sessionModel) {
        List<CorpusInfo> list = corpusService.getLibraryInfos(sessionModel);
        return ResponseEntity.ok().body(list);
    }

    @PostMapping("/add")
    @ApiOperation(value = "创建语音库信息",notes = "创建语音库信息")
    public ResponseEntity<Void> createLibraryInfo(@RequestBody LibraryInfoRequest request,
                                                  @RequestParam(value = "file", required = false) MultipartFile file,
                                                  @ApiIgnore @CurrentUser SessionModel sessionModel) throws IOException {
        corpusService.createLibraryInfo(request, sessionModel, file);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新语音库信息",notes = "更新语音库信息")
    public ResponseEntity<Void> updateLibraryInfo(@RequestBody LibraryInfoRequest request,
                                                  @RequestParam(value = "file", required = false) MultipartFile file,
                                                  @ApiIgnore @CurrentUser SessionModel sessionModel) throws IOException {
        corpusService.updateLibraryInfo(request, sessionModel, file);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "语音库信息删除",notes = "语音库信息删除")
    public ResponseEntity<String> deleteInfo(@PathVariable String id) {
        corpusService.deleteInfo(id);
        return ResponseEntity.ok().body("操作成功");
    }
}
